package com.tb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.mapper.AnswerMapper;
import com.tb.model.*;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private ACommentService aCommentService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private IllegalityService illegalityService;

    public Answer getAnswerByAnswerId(Integer answerId) {
        Answer answer = answerMapper.selectByPrimaryKey(answerId);
        answer.setUser(userService.getUserById(answer.getUserId()));
        answer.setQuestion(questionService.getQuestionById(answer.getQuestionId()));
        return answer;
    }

    public void deleteAnswerByAnswerId(int id) {
        //删除之前连带删除消息表和违规记录表中的记录
        messageService.removeRecordByTypeAndObjectId(MyConstant.ACT_ANSWER,id);
        illegalityService.removeRecordByTypeAndObjectId(MyConstant.ACT_ANSWER,id);
        //删除旗下所有评论
        List<AComment> aCommentList = aCommentService.getCommentByAnswerId(id);
        for (AComment aComment : aCommentList) {
            aCommentService.deleteAnswerCommentById(aComment.getId());
        }
        //真正删除问题记录
        answerMapper.deleteByPrimaryKey(id);
    }

    public List<Answer> getAnswerWithCommentByQuestionId(Integer questionId) {
        List<Answer> answerList = getAnswerByQuestionId(questionId);
        for (Answer answer : answerList) {
//			为答案设置用户
            answer.setUser(userService.getUserById(answer.getUserId()));
//			为答案获取评论
            answer.setaCommentList(aCommentService.getCommentByAnswerId(answer.getId()));
        }
        return answerList;
    }

    public List<Answer> getAnswerByQuestionId(Integer questionId) {
        AnswerExample example = new AnswerExample();
        AnswerExample.Criteria criteria = example.createCriteria();
        criteria.andQuestionIdEqualTo(questionId);
//		根据问题id获取答案列表
        List<Answer> answerList = answerMapper.selectByExample(example);
        return answerList;
    }

    public void addAnswer(Answer answer) {
        String content = answer.getContent();
        answer.setContent(MyUtils.purifyString(content,sensitiveWordService.getStringList()));
        answerMapper.insertSelective(answer);
    }

    public PageInfo<Answer> getAnswerByFollowListAndPageId(List<Follow> followList,int pageId) {
        //取得已关注的人的id
        List<Integer> userIdList = new ArrayList<>();
        for (Follow follow : followList) {
            userIdList.add(follow.getToId());
        }
        if (userIdList.isEmpty()) {
            return new PageInfo<>();
        }
        //取得已关注人的回答
        AnswerExample example = new AnswerExample();
        example.setOrderByClause("time desc");
        AnswerExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdIn(userIdList);
        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_ANSWER);
        List<Answer> answerList = answerMapper.selectByExample(example);
        for (Answer answer : answerList) {
            answer.setUser(userService.getUserById(answer.getUserId()));
            answer.setQuestion(questionService.getQuestionById(answer.getQuestionId()));
        }
        return new PageInfo<>(answerList);
    }

    public PageInfo<Answer> getAnswerListByUserIdAndPageId(int fromUserId,int pageId) {
        //构造一个follow，只包含ToId
        Follow follow = new Follow();
        follow.setToId(fromUserId);
        List<Follow> followList = new ArrayList<>();
        followList.add(follow);
        //从而调用已写过的函数
        return getAnswerByFollowListAndPageId(followList,pageId);
    }

    public int getCountOfAnswerByFollowList(List<Follow> followList) {
        //取得已关注的人的id
        List<Integer> userIdList = new ArrayList<>();
        for (Follow follow : followList) {
            userIdList.add(follow.getToId());
        }
        if (userIdList.isEmpty()) {
            return 0;
        }
        AnswerExample example = new AnswerExample();
        AnswerExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdIn(userIdList);
        return answerMapper.countByExample(example);
    }

    public int getCountOfAnswerByUserId(int userId) {
        //构造一个follow，只包含ToId
        Follow follow = new Follow();
        follow.setToId(userId);
        List<Follow> followList = new ArrayList<>();
        followList.add(follow);
        //从而调用已写过的函数
        return getCountOfAnswerByFollowList(followList);
    }
}
